package com.moneyanalyzer.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.moneyanalyzer.db.DatabaseHandler;
import com.moneyanalyzer.model.Category;

public class CategoryDao {
	private static CategoryDao me;
	private CategoryDao(){
		
	}
	public static CategoryDao getInstance(){
		if(me == null)
			me = new CategoryDao();
		return me;
	}
	
	public List<Category> list(Context context){
		List<Category> categories = null;
		SQLiteDatabase sqlLite = new DatabaseHandler(context).getReadableDatabase();
        String[] columns = new String[] { DatabaseHandler.KEY_CATEGORY_ID, DatabaseHandler.KEY_CATEGORY_NAME };
        Cursor cursor = sqlLite.query(DatabaseHandler.TABLE_CATEGORY, columns, null, null, null, null, null);
        if (cursor.moveToFirst()) {
        	categories = new ArrayList<Category>();
            while (cursor.isAfterLast() == false) {
            	int id = cursor.getInt(cursor.getColumnIndex(DatabaseHandler.KEY_CATEGORY_ID));
                String name = cursor.getString(cursor.getColumnIndex(DatabaseHandler.KEY_CATEGORY_NAME));
                categories.add(new Category(id, name));
                cursor.moveToNext();
            }
        }
	    return categories;
	}
	
	public Category select(int id, Context context){
		
		Category category = null;
		Cursor cursor = null;
		SQLiteDatabase sqlLite = new DatabaseHandler(context).getReadableDatabase();
		String where = DatabaseHandler.KEY_CATEGORY_ID+" = ?";
		String[] columns = new String[] { DatabaseHandler.KEY_CATEGORY_ID, DatabaseHandler.KEY_CATEGORY_NAME};
		String argumentos[] = new String[] {String.valueOf(id)};
		cursor = sqlLite.query(DatabaseHandler.TABLE_CATEGORY, columns, where, argumentos, null, null, null);
		if (cursor != null && cursor.moveToFirst()) {
			category = new Category();
			category.setId(cursor.getInt(cursor.getColumnIndex(DatabaseHandler.KEY_CATEGORY_ID)));
			category.setName(cursor.getString(cursor.getColumnIndex(DatabaseHandler.KEY_CATEGORY_NAME)));
        }
		if (cursor != null)
			cursor.close();
		return category;
	}
	
	public long insert(Category category, Context context){
		SQLiteDatabase sqlLite = new DatabaseHandler(context).getReadableDatabase();
		ContentValues content = new ContentValues();
		content.put(DatabaseHandler.KEY_CATEGORY_NAME, category.getName());
		return sqlLite.insert(DatabaseHandler.TABLE_CATEGORY, null, content);
	}
	
	public int update(Category category, Context context) throws Exception {
		SQLiteDatabase sqlLite = new DatabaseHandler(context).getWritableDatabase();
 
		ContentValues content = new ContentValues();
		content.put(DatabaseHandler.KEY_CATEGORY_NAME, category.getName());
 
        String where = DatabaseHandler.KEY_CATEGORY_ID+" = ?";
 
        String argumentos[] = { String.valueOf(category.getId()) };
 
		return sqlLite.update(DatabaseHandler.TABLE_CATEGORY, content, where, argumentos);
	}
	
	public int delete(Category category, Context context) {
        SQLiteDatabase sqlLite = new DatabaseHandler(context).getWritableDatabase();  

		String where = DatabaseHandler.KEY_CATEGORY_ID+" = ? ";
		
		String[] args = new String[] { String.valueOf(category.getId()) };
		
		return sqlLite.delete(DatabaseHandler.TABLE_CATEGORY, where, args);
	}
}
